package com.yaoli.niuke;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by siemens on 5/8/2017.
 */
public class _005_Test {
    public static void main(String[] args) {
        _005_Test test = new _005_Test();

        System.out.println(test.movingCount(5,10,10));
    }
    public int movingCount(int threshold, int rows, int cols)
    {
        thres = threshold;
        thisrow = rows - 1;
        thiscol = cols - 1;


        check(0,0);

        return count;
    }


    Set<String> visited = new HashSet<String>();
    int count=0;

    int thres=0;
    int thisrow=0;
    int thiscol=0;

    public void check(int rows,int cols){
        int total = 0;
        int row = rows;
        int col = cols;

        while(row != 0){
            total = total + row % 10;
            row = row / 10;
        }

        while(col != 0){
            total = total + col % 10;
            col = col / 10;
        }
        //System.out.println(rows+"-"+cols+" --"+total);
        //能够进入
        if(total <= thres){
            if(!visited.contains(rows+"-"+cols)){
                System.out.println(rows+"-"+cols+" --"+total);
                visited.add(rows+"-"+cols);
                count++;


                if(rows>0){
                    check(rows - 1, cols);
                }

                if(rows < thisrow){
                    check(rows + 1,cols);
                }

                if(cols > 0){
                    check(rows,cols - 1);
                }

                if(cols < thiscol){
                    check(rows,cols + 1);
                }
                //visited.remove(rows+"-"+cols);
            }
        }
    }
}
